﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using WINPE_TOOLBOX; // Assuming Utils is in this namespace

public class ToSystem
{
    private const string GUID1 = "{4a00d3c0-3a86-2d40-b468-8bb065afa321}";
    private const string GUID2 = "{3a5d9b25-3e56-7c1a-0162-d1bfe6b14acc}";
    private readonly string bcdeditPath;
    private readonly string tempPath;
    private bool isUpdate = false;
    private static readonly Config _config = Config.Instance;

    public ToSystem(string toolsPath, string clientTempPath)
    {
        bcdeditPath = Path.Combine(toolsPath, "bcdedit.exe");
        tempPath = Path.Combine(clientTempPath, "install\\peFiles\\");
    }

    public async Task InstallToSystem(Action<int> setCurrentStep, Action<string> setStepStr, Action<bool> setLockMuen)
    {
        if (!CheckIsReady()) return;

        setLockMuen(true);
        Console.WriteLine("installToSystem");
        setCurrentStep(0);
        setStepStr("正在解压WINPE源文件");

        // 解压文件
        await Utils.RunCmdAsync("unzip sourcePath destinationPath"); // Assuming a command for unzipping
        Console.WriteLine("解压完成");

        setCurrentStep(1);
        setStepStr("正在复制WINPE文件");

        // 创建目录
        Utils.MakeDir("C:\\HotPE\\");
        // 复制文件
        Utils.CopyFile("sourceFilePath", "destinationFilePath");

        // 更新PE配置文件
        var hotPEConfig = Utils.ReadHotPEConfig("C:\\");

        // 修改 INI 文件中的值
        hotPEConfig["Information"]["Installation_Method"] = "System";
        hotPEConfig["Information"]["ReleaseVersion"] = "version";

        // 将修改后的配置写回文件
        Utils.WriteHotPEConfig("C:\\", hotPEConfig);

        // 添加引导
        setCurrentStep(2);
        setStepStr("正在添加PE引导");

        string installLetter = "C:"; // 示例，替换为实际逻辑
        await Utils.RunCmdAsync($"{bcdeditPath} /create {GUID1} /d WINPE工具箱 /application osloader");
        await Utils.RunCmdAsync($"{bcdeditPath} /create {GUID2} /d WINPE工具箱 /device");

        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID2} ramdisksdidevice partition={installLetter}");
        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID2} ramdisksdipath \\HotPE\\Kernel.SDI");

        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} device ramdisk=[{installLetter}]\\HotPE\\Kernel.WIM,{GUID2}");

        if (_config.Environment.Ware.System.Firmware = "UEFI") // 替换为实际的UEFI检查
        {
            await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} path \\windows\\system32\\boot\\winload.efi");
        }
        else
        {
            await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} path \\windows\\system32\\boot\\winload.exe");
        }

        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} description WINPE工具箱");
        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} osdevice ramdisk=[{installLetter}]\\HotPE\\Kernel.WIM,{GUID2}");
        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} systemroot \\windows");
        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} detecthal Yes");
        await Utils.RunCmdAsync($"{bcdeditPath} /set {GUID1} winpe Yes");
        await Utils.RunCmdAsync($"{bcdeditPath} /displayorder {GUID1} /addlast");

        await Utils.RunCmdAsync($"{bcdeditPath} /timeout 30");

        await Utils.RunCmdAsync($"attrib {installLetter}\\HotPE +S +H /S /D");
        await Utils.RunCmdAsync($"attrib {installLetter}\\HotPE\\* +S +H /S /D");

        // 清理
        // await Utils.DelDir(tempPath);

        // 更新PE安装状态
        await CheckPEDrive();

        if (!isUpdate)
        {
            Console.WriteLine("安装到系统完成！");
        }

        setCurrentStep(-1);
        setLockMuen(false);
    }

    private bool CheckIsReady()
    {
        // 实现准备检查逻辑
        return true;
    }

    private async Task CheckPEDrive()
    {
        // Implement the logic to check PE drive
    }
}